package com.mdd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.common.CustomException;
import com.mdd.dto.SetmealDto;
import com.mdd.mapper.SetmealMapper;
import com.mdd.pojo.Category;
import com.mdd.pojo.Setmeal;
import com.mdd.pojo.SetmealDish;
import com.mdd.service.CategoryService;
import com.mdd.service.SetmealDishService;
import com.mdd.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐,同时保存菜品和套餐的关联关系
     * @param setmealDto
     */
    @Override
    public void addSetmeal(SetmealDto setmealDto) {
        //添加套餐
        save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }


    /**
     * 实现套餐分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<SetmealDto> pageList(int page, int pageSize,String name) {
        //创建分页对象
        Page<Setmeal> setmealPage = new Page<>();
        //创建条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //name如果存在就按name进行分页查询
        queryWrapper.like(name!=null,Setmeal::getName,name).orderByDesc(Setmeal::getUpdateTime);
        //分页查询
        page(setmealPage,queryWrapper);

        //创建SetmealDto分页对象
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"re cords");
        ArrayList<SetmealDto> setmealDishes = new ArrayList<>();
        setmealDtoPage.setRecords(setmealDishes);
        for (Setmeal setmeal : setmealPage.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            //根据分类id查询分类对象
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            setmealDishes.add(setmealDto);
        }
        return setmealDtoPage;
    }

    /**
     * 批量删除套餐和套餐和菜关系
     * @param ids
     */
    @Override
    @Transactional
    public void removeSetmeal(List<Long> ids) {
        //查询待删除套餐的状态，确认是否可以删除
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);
        if (count(wrapper) > 0) {
            throw new CustomException("套餐正在售卖中不可删除!");
        }
        //批量删除套餐
        removeByIds(ids);

        //创建条件构造器
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除套餐中关联的菜品
        setmealDishService.remove(queryWrapper);
    }
}
